﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure
{
    /// <summary>
    /// 字符串安全处理扩展方法
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// 安全获取字符串值，如果为null则返回空字符串
        /// </summary>
        public static string SafeToString(this object value)
        {
            return value == null || value == DBNull.Value ? string.Empty : value.ToString();
        }
    }

    public class BaseRepository<T>: IRepository<T> where T : class
    {
        /// <summary>
        /// 上下文
        /// </summary>
        protected readonly DataDbContext _context;
        protected readonly DbSet<T> _dbSet;

        public BaseRepository(DataDbContext context)
        {
            _context = context;
            _dbSet = context.Set<T>();
        }

        /// <summary>
        /// 查询所有方法
        /// </summary>
        /// <returns>返回符号条件的数据</returns>
        public IQueryable<T> GetAll()
        {
            return _dbSet.AsQueryable();
        }

        

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>添加结果</returns>
        public async Task<int> Add(T entity)
        {
            await _dbSet.AddAsync(entity);

            return await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 异步添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>添加结果</returns>
        public async Task<int> AddAsync(T entity)
        {
            await _dbSet.AddAsync(entity);
            return await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>修改结果</returns>
        public async Task<int> Update(T entity)
        {
            _dbSet.Update(entity);
            return await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 异步修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>修改结果</returns>
        public async Task<int> UpdateAsync(T entity)
        {
            _dbSet.Update(entity);
            return await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 添加多条
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>添加结果</returns>
        public async Task<int> AddRange(List<T> entity)
        {
            await _dbSet.AddRangeAsync(entity);
            return await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回多条结果</returns>
        public async Task<int> UpdateRange(List<T> entity)
        {
            _dbSet.UpdateRange(entity);
            return await _context.SaveChangesAsync();
        }

        public Task<T> GetByIdAsync(object id)
        {
            throw new NotImplementedException();
        }
        
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>删除结果</returns>
        public async Task<int> DeleteAsync(T entity)
        {
            _dbSet.Remove(entity);
            return await _context.SaveChangesAsync();
        }
    }
}
